/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function DocumentUtils(){
	this.jsjava_class="jsorg.eob.document.DocumentUtils";
}

/**
 * Get elements by classname of HTML tags
 * @param domObj object in document model
 * @param tagName HTML tag name
 * @param className css class name
 */
DocumentUtils.getElementsByClassName=function(domObj,tagName,className){
	return DocumentUtils.getElementsByProperty(domObj,tagName,"className",className);
};

/**
 * Get elements by attribute of HTML tags
 * @param domObj object in document model
 * @param tagName HTML tag name
 * @param attrName css attribute name
 * @param attrValue css attribute value
 */
DocumentUtils.getElementsByAttribute=function(domObj,tagName,attrName,attrValue){
	if(attrName=="className"&&!BrowserUtils.isIE()){
		attrName="class";
	}
	var elems=new ArrayList();
	var tagElems=domObj.getElementsByTagName(tagName.toUpperCase());
	for(var i=0;i<tagElems.length;i++){
		var tagElem=tagElems[i];
		var tagAttrValue=tagElem.getAttribute(attrName);
		if(tagAttrValue==attrValue){
			elems.add(tagElem);
		}
	}
	return elems.toArray();
};

/**
 * Get elements by property of HTML tags
 * @param domObj object in document model
 * @param tagName HTML tag name
 * @param propName css property name
 * @param propValue css property value
 */
DocumentUtils.getElementsByProperty=function(domObj,tagName,propName,propValue){
	var elems=new ArrayList();
	var tagElems=domObj.getElementsByTagName(tagName.toUpperCase());
	for(var i=0;i<tagElems.length;i++){
		var tagElem=tagElems[i];
		var tagPropValue=eval("tagElem."+propName);
		if(tagPropValue==propValue){
			elems.add(tagElem);
		}
	}
	return elems.toArray();
};

/**
 * Get frame sub window object
 * @param winObj the object of the window where the frame is on
 * @param frameName the name of the frame
 */
DocumentUtils.getFrameWindowObjectByName=function(winObj,frameName){
    var frameObj=eval("winObj."+frameName);
    return frameObj.window;
};

/**
 * Get frame sub window object
 * @param winObj the object of the window where the frame is on
 * @param frameId the id of the frame
 */
DocumentUtils.getFrameWindowObjectById=function(winObj,frameId){
    var frameTagObj=DocumentUtils.getFrameTagObjectById(winObj,frameId);
    return frameTagObj.contentWindow;
};

/**
 * Get frame tag object
 * @param winObj the object of the window where the frame is on
 * @param frameId the id of the frame
 */
DocumentUtils.getFrameTagObjectById=function(winObj,frameId){
	return winObj.document.getElementById(frameId);
};

/**
 * Get frame tag object
 * @param winObj the object of the window where the frame is on
 * @param frameId the id of the frame
 */
DocumentUtils.getFrameTagObjectByName=function(winObj,frameName){
	var elems=winObj.document.getElementsByName(frameName);
	return elems[0];
};

/**
 * Get the top window of the current window
 */
DocumentUtils.getTopWindowObjectOfCurrent=function(){
	return window.top;
};

/**
 * Get the parent window of the current window
 */
DocumentUtils.getParentWindowObjectOfCurrent=function(){
	return window.parent;
};

/**
 * Get the operner window from where the current window is opened
 */
DocumentUtils.getOpenerWindowObjectOfCurrent=function(){
    return window.opener;
};

/**
 * Get the element area object
 * @param elemObj
 */
DocumentUtils.getElementRectangle=function(elemObj){
	if(elemObj==undefined){
		return null;
	}
	var rect=null;
	if(BrowserUtils.isIE()){
		var orect=elemObj.getBoundingClientRect();
		var x=orect.left;
		var y=orect.top;
		var width=orect.right-orect.left;
		var height=orect.bottom-orect.top;
		rect=new Rectangle(x,y,width,height);
	}else{
		var orect=document.getBoxObjectFor(elemObj);
		var x=orect.x;
		var y=orect.y;
		var width=orect.width;
		var height=orect.height;
		rect=new Rectangle(x,y,width,height); 
	}
	return rect;
};

/**
 * Get the operner window from where the current window is opened
 */
DocumentUtils.getOpenerWindowObjectOfCurrent=function(){
    return window.opener;
};

/**
 * Judge whether the element is a HTML text object
 * @param elemObj
 */
DocumentUtils.isHTMLTextObject=function(elemObj){
	if(DocumentUtils.isHTMLTextArea(elemObj)||DocumentUtils.isHTMLTextField(elemObj)){
		return true;
	}
	return false;
};

/**
 * Judge whether the element is a HTML textarea object
 * @param elemObj
 */
DocumentUtils.isHTMLTextArea=function(elemObj){
	var flag=elemObj==undefined||elemObj==null||elemObj.parentNode==undefined||elemObj.tagName==undefined;	
	if(flag){
		return false;
	}
	var tagName=elemObj.tagName.toUpperCase();
	if(tagName!="TEXTAREA"){
		return false;
	}	
	return true;
};

/**
 * Judge whether the element is a HTML textarea object
 * @param elemObj
 */
DocumentUtils.isHTMLTextField=function(elemObj){
	var flag=elemObj==undefined||elemObj==null||elemObj.parentNode==undefined||elemObj.tagName==undefined;	
	if(flag){
		return false;
	}
	var tagName=elemObj.tagName.toUpperCase();
	if(tagName!="INPUT"||tagName=="INPUT"&&elemObj.type.toUpperCase()!="TEXT"){
		return false;
	}	
	return true;
};

/**
 * Set dom disabled or not
 * @param dom
 * @param flag
 */
DocumentUtils.setDisnabled=function(dom,flag){
	dom.disabled=flag;
};

/**
 * Set dom editable or not
 * @param dom
 * @param flag
 */
DocumentUtils.setEditable=function(dom,flag){
	dom.readOnly=!flag;
};

/**
 * Set text copy-enabled or not within dom area
 * @param dom
 * @param flag
 */
DocumentUtils.setCanCopy=function(dom,flag){	
	EventUtils.addDomEvent(dom,"copy",new Function("return "+new Boolean(flag)),false);
};

/**
 * Set text paste-enabled or not within dom area
 * @param dom
 * @param flag
 */
DocumentUtils.setCanPaste=function(dom,flag){
	EventUtils.addDomEvent(dom,"paste",new Function("return "+new Boolean(flag)),false);
};

/**
 * Set text cut-enabled or not within dom area
 * @param dom
 * @param flag
 */
DocumentUtils.setCanCut=function(dom,flag){
	EventUtils.addDomEvent(dom,"cut",new Function("return "+new Boolean(flag)),false);
};

/**
 * Set text context menu enabled or not within dom area
 * @param dom
 * @param flag
 */
DocumentUtils.setCanContextMenu=function(dom,flag){
	EventUtils.addDomEvent(dom,"contextmenu",new Function("return "+new Boolean(flag)),false);
};

/**
 * Set the width of element
 * @param dom
 * @param width
 */
DocumentUtils.setDomWidth=function(dom,width){
	if(dom.width!=undefined){
		dom.width=width;
	}else if(dom.style.width!=undefined){
		dom.style.width=width;
	}
};

/**
 * Set the height of element
 * @param dom
 * @param width
 */
DocumentUtils.setDomHeight=function(dom,height){
	if(dom.height!=undefined){
		dom.height=height;
	}else if(dom.style.height!=undefined){
		dom.style.height=height;
	}
};

/**
 * Cancel document selections
 */
DocumentUtils.cancelSelection=function(){
	if(BrowserUtils.isIE()){
		document.selection.empty();
	}else{
		window.getSelection().removeAllRanges();
	}
};

/**
 * Cancel document selections
 */
DocumentUtils.isContainsNode=function(node1,node2){
	var count=0;
    var srcNode = node2;
    while(srcNode){
        if(count>100){
            return false;
        }
        if(srcNode == node1){
            return true;
        }
        count++;
        srcNode = srcNode.parentNode;
    }
    return false;
};